package com.lg.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.lg.model.AngelCandidate;
import com.lg.model.AngelUser;
import com.lg.model.StPromoter;
import com.lg.serivce.AngelCandidateService;
import com.lg.serivce.AngelUserService;
import com.lg.serivce.StPromoterService;

@Controller
@SessionAttributes("user")
public class RegisterController extends BaseController {

	@Resource(name = "angelUserService")
	private AngelUserService angelUserService;
	@Resource(name = "angelCandidateService")
	private AngelCandidateService angelCandidateService;
	@Resource(name = "stPromoterService")
	private StPromoterService stPromoterService;

	/**
	 * 进入用户注册界面
	 */
	@RequestMapping("/reg.do")
	public String reg(HttpServletRequest request, ModelMap map) {
		// 预留天使人气
		List<AngelCandidate> list = angelCandidateService
				.findIndexUserVoteNum();
		map.addAttribute("voteList", list);
		return "register";
	}

	/**
	 * 用户注册
	 */
	@RequestMapping("/register.do")
	public String register(AngelUser angelUser, String link,
			HttpServletRequest request, ModelMap map) {
		if (angelUserService.addUserinfo(angelUser)) {
			angelUser = angelUserService.findUserByRegid(angelUser.getRegId());
			HttpSession session = request.getSession(false);
			session.setAttribute("user", angelUser);
			if (link != null && !"".equals(link))
				return "redirect:" + link;
			return "tianshi"; // 转到天使风采页面
		} else {
			return "register";
		}
	}

	/**
	 * 用户登陆
	 */
	@RequestMapping("/login.do")
	public String login(String selType, String regid, String pwd, String link,
			HttpServletRequest request, ModelMap map) {
		if (selType == null || regid == null)
			return "index";
		if (!"1".equals(selType) && !"2".equals(selType) || link == null)
			return "index";
		HttpSession session = request.getSession();
		// 网友登录
		if (selType != null && "1".equals(selType)) {
			AngelUser user = angelUserService.findUserInLogin(regid, pwd);
			if (user != null) {
				session.setAttribute("user", user);
				request.setAttribute("msg", "登录成功!");
			} else {
				request.setAttribute("msg", "用户名或密码错误,请重新输入!");
			}
		}
		// 选手登录
		if (selType != null && "2".equals(selType)) {
			StPromoter user = stPromoterService.findPromoterUser(regid, pwd);
			if (user != null) {
				session.setAttribute("user", user);
				request.setAttribute("msg", "登录成功!");
			} else {
				request.setAttribute("msg", "用户名或密码错误,请重新输入!");
			}
		}
		return "forward:" + link;
	}

	/**
	 * 用户名校验
	 */
	@RequestMapping("/check.do")
	public void check(String regId, HttpServletResponse response) {
		String type = null;
		if (!angelUserService.findUserIsExist(regId)) {
			type = "1";
		} else {
			type = "0";
		}
		try {
			ajaxOutput(response, type);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 用户完善个人资料
	 */
	@RequestMapping("/candidatereg.do")
	public String candidatereg(String promoterId, String regId,
			AngelCandidate angelCandidate, String imagefile1,
			String imagefile2, String imagefile3,String imagefile4,String imagefile5,String imagefile6,String imagefile7,String imagefile8,HttpServletResponse response, ModelMap map) {
		if (promoterId == null || regId == null) {
			return "index";
		}
		StPromoter promoter = stPromoterService.findLoadPromoterUser(promoterId, regId);
		if (promoter != null) { // 如果用户存在则可以进行完善个人资料操作
			angelCandidate.setPromoterId(promoterId);
			if(imagefile1 != null && !"".equals(imagefile1)){
				
			}
			return "xuanshou"; // 跳到选手主页
		} else {
			return "index";
		}

	}

	/**
	 * 加载选手用户信息
	 */
	@RequestMapping("/cadidateload.do")
	public String loadPromoter(String promoterid, String regid,
			HttpServletResponse response, ModelMap map) {
		// 预留天使人气
		List<AngelCandidate> list = angelCandidateService.findIndexUserVoteNum();
		map.addAttribute("voteList", list);
		StPromoter promoter = stPromoterService.findLoadPromoterUser(promoterid, regid);
		if (promoter == null)
			return "redirect:tianshi.do"; // 天使风采页面
		map.addAttribute("promoter", promoter);
		return "completedata";
	}

	/**
	 * 选手昵称唯一校验
	 */
	@RequestMapping("/nickcheck.do")
	public void nickcheck(String nkName, HttpServletResponse response) {
		String type = null;
		if (!angelCandidateService.findCandidateIsExist(nkName)) {
			type = "1";
		} else {
			type = "0";
		}
		try {
			ajaxOutput(response, type);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
